﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ajax.Samples;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Threading;

namespace MBWebService.DevServer
{
    class Program
    {
        static bool loading = false;
        static MBService Service;

        static void Main(string[] args)
        {
            Console.WriteLine("Starting service");
            Console.Write("Loading.");
            loading = true;
            ThreadPool.QueueUserWorkItem((o) =>
            {
                while (loading)
                {
                    Console.Write(".");
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
            );
            var success = LoadService();

            loading = false;
            Console.WriteLine(string.Empty);
            if (success)
            {
                Console.WriteLine("Service started");

                Console.WriteLine("[Press Enter to stop service]");
            }
            else
            {
                Console.WriteLine("Failure starting service");
                Console.WriteLine("[Press Enter to close]");
            }

            while (Console.ReadKey().Key != ConsoleKey.Enter) { };
        }

        private static bool LoadService()
        {
            bool retVal = false;

            try
            {
                Service = new MBService();
                Service.StartService();
                retVal = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return retVal;
        }
    }
}
